package com.controller;

import com.bean.Log;
import com.common.Result;
import com.service.LogService;
import com.utils.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/logs")
public class LogController {
    @Autowired
    private LogService logService;

    @GetMapping("/{logId}")
    public Result<Log> getLogById(@PathVariable Integer logId) {
        Log log = logService.getLogById(logId);
        if (log != null) {
            return Result.success(log);
        }
        return Result.fail("日志信息不存在");
    }

    @GetMapping
    public Result<PageInfo<Log>> getLogsWithKeyword(
            @RequestParam(required = false) String keyword,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        PageInfo<Log> pageInfo = logService.getLogsWithKeyword(keyword, pageNum, pageSize);
        return Result.success(pageInfo);
    }

    @PostMapping
    public Result<Boolean> insertLog(@RequestBody Log log) {
        boolean result = logService.insertLog(log);
        if (result) {
            return Result.success(true);
        }
        return Result.fail("插入日志信息失败");
    }

    @PutMapping
    public Result<Boolean> updateLog(@RequestBody Log log) {
        boolean result = logService.updateLog(log);
        if (result) {
            return Result.success(true);
        }
        return Result.fail("更新日志信息失败，日志可能不存在");
    }

    @DeleteMapping("/{logId}")
    public Result<Boolean> deleteLogById(@PathVariable Integer logId) {
        boolean result = logService.deleteLogById(logId);
        if (result) {
            return Result.success(true);
        }
        return Result.fail("删除日志信息失败，日志可能不存在");
    }
}